package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.common.MessageConst;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/*
 * @Author 牛月洲
 * @Date 2020/4/15
 * @Version v1.0
 * @Description  指定时间段内展示会员数量报表
*/
@RestController
@RequestMapping("/member")
@Slf4j
public class MemberController {
    @Reference
    private MemberService memberService;

    @RequestMapping("/findMemberByDate")
    public Result findMemberByDate(@RequestParam("beginQueryDate") String beginQueryDate , @RequestParam("endQueryDate")String endQueryDate){

            Integer i = memberService.findMemberByDate(beginQueryDate,endQueryDate);
            if (i==null){
                return new Result(false,"日期格式错误,请重新填写正确的格式");
            }
            return new Result(true,"查询指定时间段内的会员成功",i);

    }

    /*
     * @Author 牛月洲
     * @Date 2020/4/15
     * @Version v1.0
     * @Description 导出excel表
    */
    @RequestMapping("/getExcel")
    public Result getExcel(HttpServletResponse response,@RequestParam("beginQueryDate") String beginQueryDate , @RequestParam("endQueryDate")String endQueryDate){

        try {
            //调用Service方法获取统计数据
            Map map = memberService.getExcel(beginQueryDate,endQueryDate);
            //获得当前选中段日期
            String date2date = (String) map.get("date2date");
            //获取会员有关数据
            Integer Date2DateMember = (Integer) map.get("Date2DateMember");
            Integer todayNewMember = (Integer) map.get("todayNewMember");
            Integer thisWeekNewMember = (Integer) map.get("thisWeekNewMember");
            Integer thisMonthNewMember = (Integer) map.get("thisMonthNewMember");
            Integer totalMember = (Integer) map.get("totalMember");

            //利用POIUtils工具类将数据写入到Excel
            InputStream inputStream = ReportController.class.getClassLoader().getResourceAsStream("Date2DateMemberCount.xlsx");
            //创建Workbook对象，获取工作表
            Workbook workbook = new XSSFWorkbook(inputStream);
            Sheet sheet = workbook.getSheetAt(0);
            //获取报表日期行
            Row row = sheet.getRow(4);
            row.getCell(4).setCellValue(date2date);
            row = sheet.getRow(5);
            row.getCell(4).setCellValue(Date2DateMember);
            //获取新增会员及会员总数行
            row = sheet.getRow(7);
            row.getCell(4).setCellValue(todayNewMember);
            row.getCell(6).setCellValue(totalMember);
            //获取本周新增会员及本月新增会员行
            row = sheet.getRow(8);
            row.getCell(4).setCellValue(thisWeekNewMember);
            row.getCell(6).setCellValue(thisMonthNewMember);

            //通过输出流进行文件下载
            ServletOutputStream out = response.getOutputStream();
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-Disposition","attachment;filename="+"会员数量报表"+"_report.xlsx");
            workbook.write(out);
            out.close();
            workbook.close();
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConst.ACTION_FAIL);
        }
    }
}


